package net.zio.kamenriderzioaddon.client.renderer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import software.bernie.geckolib3.core.IAnimatable;
import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
import software.bernie.geckolib3.core.util.Color;
import software.bernie.geckolib3.geo.render.built.GeoModel;
import software.bernie.geckolib3.model.AnimatedGeoModel;
import software.bernie.geckolib3.renderers.geo.GeoArmorRenderer;
import software.bernie.geckolib3.util.EModelRenderCycle;
import net.zio.kamenriderzioaddon.item.GeizItem;
import net.zio.kamenriderzioaddon.item.model.GeizModel;

import java.util.Arrays;

public class GeizArmorRenderer extends GeoArmorRenderer<GeizItem> {
    private static final ResourceLocation LAYER1 = 
        new ResourceLocation("kamen_rider_zio_addon", "textures/entities/geiz_highlight.png");

    public GeizArmorRenderer() {
        super(new GeizModel());
        this.headBone = "armorHead";
        this.bodyBone = "armorBody";
        this.rightArmBone = "armorRightArm";
        this.leftArmBone = "armorLeftArm";
        this.rightLegBone = "armorRightLeg";
        this.leftLegBone = "armorLeftLeg";
        this.rightBootBone = "armorRightBoot";
        this.leftBootBone = "armorLeftBoot";
    }

    @Override
    public void render(float partialTick, PoseStack poseStack, VertexConsumer buffer, int packedLight) {
        // 获取模型和动画事件
        GeoModel model = this.getGeoModelProvider().getModel(
            this.getGeoModelProvider().getModelLocation(this.currentArmorItem)
        );
        
        AnimationEvent animationEvent = new AnimationEvent(
            (IAnimatable) this.currentArmorItem,
            0.0F, 0.0F,
            Minecraft.getInstance().getDeltaFrameTime(),
            false,
            Arrays.asList(this.itemStack, this.entityLiving, this.armorSlot)
        );

        // 准备矩阵变换
        poseStack.pushPose();
        poseStack.translate(0.0, 1.5, 0.0);
        poseStack.scale(-1.0F, -1.0F, 1.0F);
        
        // 设置动画和渲染周期
        this.getGeoModelProvider().setCustomAnimations(
                this.currentArmorItem,
                this.getInstanceId(this.currentArmorItem),
                animationEvent
        );
        this.setCurrentModelRenderCycle(EModelRenderCycle.INITIAL);
        this.fitToBiped();
        
        // 渲染基础纹理层
        Color renderColor = this.getRenderColor(
            this.currentArmorItem,
            partialTick,
            poseStack,
            null,
            buffer,
            packedLight
        );
        
        RenderType renderType = this.getRenderType(
            this.currentArmorItem,
            partialTick,
            poseStack,
            null,
            buffer,
            packedLight,
            this.getTextureLocation(this.currentArmorItem)
        );
        
        this.render(
            model,
            this.currentArmorItem,
            partialTick,
            renderType,
            poseStack,
            Minecraft.getInstance().renderBuffers().bufferSource(),
            buffer,
            packedLight,
            OverlayTexture.NO_OVERLAY,
            renderColor.getRed() / 255.0F,
            renderColor.getGreen() / 255.0F,
            renderColor.getBlue() / 255.0F,
            renderColor.getAlpha() / 255.0F
        );
        
        // 渲染发光层
        RenderType glowType = RenderType.eyes(LAYER1);
        VertexConsumer glowBuffer = Minecraft.getInstance().renderBuffers().bufferSource().getBuffer(glowType);
        
        this.render(
            model,
            (GeizItem) this.currentArmorItem,
            partialTick,
            glowType,
            poseStack,
            Minecraft.getInstance().renderBuffers().bufferSource(),
            glowBuffer,
            15728880, // 最大光照值（确保发光效果在任何环境下都可见）
            OverlayTexture.NO_OVERLAY,
            1.0F, 1.0F, 1.0F, 1.0F // 纯白色（保持纹理原始颜色）
        );
        
        // 恢复矩阵状态
        poseStack.popPose();
    }

    @Override
    public RenderType getRenderType(GeizItem animatable, float partialTick, PoseStack poseStack, 
                                   MultiBufferSource bufferSource, VertexConsumer buffer, int packedLight, 
                                   ResourceLocation texture) {
        return RenderType.entityTranslucent(getTextureLocation(animatable));
    }
}